<?php

include "jpgraph/jpgraph.php";
include "jpgraph/jpgraph_line.php";
include "config.php";
include "attributes.php";

mysql_connect($dbhost,$dbusername,$dbpassword);
mysql_select_db($dbname);

$ogame_playerid = (int)$_GET['playerid'];


if ($_GET['typ'] == "month") {
	$query = "SELECT a1.date_added as datevalue, avg(a1.score) as allydata, avg(a2.score) as playerdata ".
	"FROM $allyhistory a1 LEFT JOIN $allyhistory a2 ON a1.date_added = a2.date_added AND a2.ogame_playerid='$ogame_playerid' ".
	"WHERE a1.date_added > DATE_SUB(CURDATE(),INTERVAL 30 DAY) ".
	"GROUP BY datevalue ".
	"ORDER BY datevalue";
	$res = mysql_query($query) or die(mysql_error());
	if (mysql_num_rows($res) > 0) {
		// fill data
		$ydata = array();
		$ydata2 = array();
		$xdata = array();
		$tmp_time = "";
		while ($line = mysql_fetch_object($res)) {
			if (empty($tmp_time)) $tmp_time = strtotime($line->datevalue);
			if (strtotime($line->datevalue) - $tmp_time > 86400) {
				// more than one day between without update
				$next_day = $tmp_time;
				while ($next_day < strtotime($line->datevalue)-86400) {
					array_push($ydata,"-");
					array_push($ydata2,"-");
					array_push($xdata,date("m-d",$next_day+86400));
					$next_day = $next_day+86400;
				}
			}

			if ($line->playerdata == null) {
				array_push($ydata,"-");
			} else {
				array_push($ydata,$line->playerdata);
			}
			array_push($ydata2,$line->allydata);
			array_push($xdata,substr($line->datevalue,5,5));


			$tmp_time = strtotime($line->datevalue);
		}
	}

} elseif ($_GET['typ'] == "all") {
	// get ally average for all time
	$query = "SELECT substring(a1.date_added,1,7) as datevalue, avg(a1.score) as allydata, avg(a2.score) as playerdata ".
	"FROM $allyhistory a1 LEFT JOIN $allyhistory a2 ON a1.date_added = a2.date_added AND a2.ogame_playerid='$ogame_playerid' ".
	"GROUP BY datevalue ".
	"ORDER BY datevalue";
	$res = mysql_query($query) or die(mysql_error());
	if (mysql_num_rows($res) > 0) {
		// fill data
		$ydata = array();
		$ydata2 = array();
		$xdata = array();
		while ($line = mysql_fetch_object($res)) {

			if ($line->playerdata == null) {
				array_push($ydata,"-");
			} else {
				array_push($ydata,$line->playerdata);
			}
			array_push($ydata2,$line->allydata);
			array_push($xdata,$line->datevalue);

		}
	}	
	
	
/*	$query = "SELECT avg(score) as avg_allyscore, substring(date_added,1,7) as monthly FROM $allyhistory GROUP BY monthly ORDER BY monthly asc";
	$res = mysql_query($query) or die(mysql_error());
	if (mysql_num_rows($res) > 0) {
		// fill data
		$ydata2 = array();
		$xdata = array();
		while ($line = mysql_fetch_object($res)) {
			array_push($ydata2,$line->avg_allyscore);
			array_push($xdata,$line->monthly);
		}
	}
	$query = "SELECT avg(score) as avgscore, substring(date_added,1,7) as monthly FROM $allyhistory WHERE ogame_playerid='$ogame_playerid' GROUP BY monthly";
	$res = mysql_query($query) or die(mysql_error());
	if (mysql_num_rows($res) > 0) {
		// fill data
		$ydata = array();
		while ($line = mysql_fetch_object($res)) {
			array_push($ydata,$line->avgscore);
			array_push($xdata,$line->monthly);
		}
	}*/
}

if (isset($xdata) && isset($ydata) && is_array($xdata) && is_array($ydata) && count($ydata) > 1 && count($xdata) > 1) {
	// Create graph
	$graph  = new Graph(550, 250,"auto");
	$graph->SetScale( "textlin");

	if (isset($_GET['text']) && !empty($_GET['text'])) {
		$graph->title-> Set( urldecode($_GET['text']) );
		$graph->title->SetColor('#c4c4c4');
	}

	// set colors
	$graph->SetMarginColor('#415680');
	$graph->SetBackgroundGradient('#344566','#203041',GRAD_HOR,BGRAD_PLOT);
	$graph->img->SetMargin(80,20 ,20,60);

	$graph->yaxis->SetLabelFormatCallback('yLabelFormat');
	$graph->yaxis->SetColor('#c4c4c4');

	$graph->xaxis->SetColor('#c4c4c4');
	$graph->xaxis->SetTextLabelInterval(1); // show every date
	$graph->xaxis->SetLabelAngle(90);
	$graph->xaxis->SetTickLabels($xdata);

	// Create the linear plots
	$lineplot = new LinePlot($ydata);
	$lineplot->SetColor("lightgreen");
	$graph->legend->SetFillColor('#203041');
	$graph->legend->SetShadow(true);
	$graph->legend->SetColor('#c4c4c4');
	$graph->Add( $lineplot);
	if (isset($ydata2) && is_array($ydata2)) {
		$lineplot2 = new LinePlot($ydata2);
		$lineplot2->SetColor("orange");
		$graph->Add( $lineplot2);
		$lineplot->SetLegend(urldecode($_GET['playername']));
		$lineplot2->SetLegend(urldecode($_GET['ally_avg']));

		$graph ->legend->Pos(0.0,0.0,"right");
	}

	// Display the graph
	$graph->Stroke();

}

function yLabelFormat($aLabel) {
	if (is_numeric($aLabel)) return number_format($aLabel,0,",",".");
	else return $aLabel;
}


?>